package org.jeecg.modules.wo.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 对接抗疫情Api的工具类
 */
public class KyqApiUtils {
    /**
     * 抗疫情接口1
     * 参数为：
     * {
     * 	"userNo": "",人员编号
     * 	"name": "",人员姓名
     * 	"temperature": 36.7,体温
     * 	"sn": "G60838985",抗疫情中登记的设备sn
     * 	"serialNo": 23,设备发起的序列化
     * 	"direction ": 1,方向
     * 	"verifyType": 2,认证方式 2为人脸
     * 	"picture": "data:image/jpg;base64,",
     * 	"cardNo": ""
     * }
     */
    public static final String SN="G60838985";
    public static final String API_REMOTECHECK="http://106.60.78.73:9000/turnstile/remoteCheck";
    public static final String API_NUCLEICACIDTEST="http://106.60.78.73:9000/turnstile/nucleicAcidTest";//核酸接口
    /**获取鉴权信息**/
    public static final String GATEWAY_APPID="0d70ad07951240db9d01761534bb7750";
    public static final String API_TOKENTEST="http://106.60.78.76:30500/auth/tokenTest";
    /**通过抗疫码获取人员核酸检测数据**/
    public static final String API_PERSONNUCLEICACIDRESULTSBYPERSONID="http://106.60.78.76:30500/api/zdwwDataQuery/personNucleicAcidResultsByPersonId";

    public static JSONObject getToken(){
        Map<String,String> header=new HashMap<String,String>();
        header.put("gateway_appid",GATEWAY_APPID);
        return JSONObject.parseObject(HttpUtils.doPost(API_TOKENTEST,header,null));
    }

    public static JSONObject getInspByKyqCode(String kyqCode){
        JSONObject rsObj=null;
        if(StringUtils.isBlank(kyqCode)){
            JSONObject tokenObj=getToken();//获取鉴权信息
            if(tokenObj!=null&&tokenObj.getInteger("code")==200){
                JSONObject bodyObj=tokenObj.getJSONObject("body");
                Map<String,String> header=new HashMap<String,String>();
                header.put("gateway_sig",bodyObj.getString("gateway_sig"));
                header.put("gateway_appid",GATEWAY_APPID);
                header.put("gateway_rtime",bodyObj.getString("gateway_rtime"));
                rsObj=JSONObject.parseObject(HttpUtils.doGet(API_PERSONNUCLEICACIDRESULTSBYPERSONID
                        +"?personId="+kyqCode,header));
            }
        }
        return rsObj;
    }

    public static void main(String[] args) {
        //KyqApiUtils.getInspByKyqCode("kcSF5JJR ");
        /*Map<String,String> header=new HashMap<String,String>();
        header.put("gateway_appid",GATEWAY_APPID);
        HttpUtils.doPost(API_TOKENTEST,header,null);*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date now = new Date();// 当前时间
        int s = 7*24*60*60;// 将10分钟转换为秒
        calendar.setTime(now);
        calendar.add(Calendar.SECOND, -s);// 添加s秒
        String formatTime1 = sdf.format(calendar.getTime());
        Date rDate= calendar.getTime();
        System.out.println("date1:" + formatTime1);
    }


    public static JSONObject remoteCheck(JSONObject prams){
        return JSONObject.parseObject(HttpUtils.doPost(API_REMOTECHECK,null,prams));
    }
    public static JSONObject nucleicAcidTest(JSONObject prams){
        return JSONObject.parseObject(HttpUtils.doPost(API_NUCLEICACIDTEST,null,prams));
    }

    /**
     * 人脸核验
     * @param faceBase64
     * @return
     */
    public static JSONObject faceCheck(String faceBase64){
        JSONObject prams=new JSONObject();
        prams.put("userNo","");
        prams.put("cardNo","");
        prams.put("name","");
        prams.put("temperature",36.7);
        prams.put("sn",SN);
        prams.put("serialNo",1);
        prams.put("direction",1);
        prams.put("verifyType",2);
        prams.put("picture",faceBase64);
        return JSONObject.parseObject(HttpUtils.doPost(API_REMOTECHECK,null,prams));
    }

    public static JSONObject nucleicAcidTest(int verifyType,String identifyNo,int visitorType,String address,String picture){
        JSONObject prams=new JSONObject();
        prams.put("sn",SN);
        prams.put("verifyType",verifyType);
        prams.put("identifyNo",identifyNo.trim());
        prams.put("visitorType",visitorType);
        prams.put("turnstileAddress",address);
        prams.put("picture",picture);
        return JSONObject.parseObject(HttpUtils.doPost(API_NUCLEICACIDTEST,null,prams));
    }

    //接口调用方法


}
